import {
  mergeOptions,
  convertColorFormat,
  pickWorldPositionByMouse,
  vec3ToJson,
  jsonToVec3
} from '../core/common.js';
import global from '../core/global.js';
import { changeMaterialLight } from '../core/scene.js';
const Vertical = {
  center: 'CENTER_V',
  bottom: 'BOTTOM',
  baseLine: 'BASELINE',
  top: 'TOP'
};
const Horizontal = {
  center: 'CENTER_H',
  left: 'LEFT',
  right: 'RIGHT'
};
let collection = null;
let Billboard = {
  add(opt, cb) {
    let defaults = {
      position: null,
      text: '',
      iconUrl: '', //图片
      mix: false,
      objectName: '',
      style: {
        font: 'Microsoft YaHei',
        fontSize: 12,
        color: '#fff',
        strokeColor: '#000',
        lineToGround: false,
        lineColor: '#ffffff',
        backgroundColor: '#00000000',
        align: 'center',
        verticalAlign: 'center',
        textAlign: 'center',
        textVertical: 'center',
        offset: [0, 0],
        iconSize: [18, 18],
        altitude: 0,
        altitudeMethod: 'RelativeToTerrain',
        fontBold: false,
        fontItalic: false,
        fontUnderline: false
      }
    };
    opt = mergeOptions(defaults, opt);
    if (!opt.position) {
      throw new TypeError('position参数需传入坐标');
    }
    const style = opt.style;
    let bbEntity = new SSmap.BillboardEntity();
    let position = jsonToVec3(opt.position);
    bbEntity.position = position;
    if (opt.iconUrl) {
      bbEntity.url = opt.iconUrl;
      bbEntity.imageWidth = opt.iconWidth ? opt.iconWidth : style.iconSize[0];
      bbEntity.imageHeight = opt.iconHeight
        ? opt.iconHeight
        : style.iconSize[1];
    }
    bbEntity.scale = opt.scale == undefined ? 1.0 : opt.scale; //比例
    if (opt.text) {
      bbEntity.text = options.text;
      bbEntity.font = style.font;
      bbEntity.fontSize = style.fontSize;
      bbEntity.fontBold = style.fontBold;
      bbEntity.fontItalic = style.fontItalic;
      bbEntity.fontUnderline = style.fontUnderline;
      bbEntity.textHorizontal = SSmap.Horizontal[Horizontal[style.textAlign]];
      bbEntity.textVertical = SSmap.Vertical[Vertical[style.textVertical]];
    }
    bbEntity.enabled == undefined ? true : opt.enabled;
    bbEntity.offset = SSmap.Cartesian2.create(style.offset[0], style.offset[1]);
    bbEntity.horizontal = SSmap.Horizontal[Horizontal[style.align]];
    bbEntity.vertical = SSmap.Vertical[Vertical[style.verticalAlign]];
    bbEntity.setScaleByDistance(SSmap.Vector4.create(0, 5.0, 1.0e4, 1.0));
    bbEntity.setAltitudeMethod(SSmap.AltitudeMethod[style.altitudeMethod]);
    bbEntity.setAltitude(style.altitude);
    let color = convertColorFormat(style.color);
    bbEntity.fontColor = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );
    color = convertColorFormat(style.strokeColor);
    bbEntity.strokeColor = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );
    bbEntity.tag = opt.tag || '';
    opt.tag ? bbEntity.addProperty('tag', opt.tag) : '';
    const userProps = opt.userProps;
    for (const key in userProps) {
      if (Object.prototype.hasOwnProperty.call(userProps, key)) {
        const value = String(userProps[key]);
        bbEntity.addProperty(key, value);
      }
    }
    collection = collection ? collection : SSmap.BillboardCollection.Instance();
    bbEntity.setCollection(collection);
    collection.setDepthTest(true);
    let id = global.BBEntityStore.add(bbEntity, opt.id);
    cb && cb(id);
    return id;
  },
  pick(x, y, cb) {
    let scene = GlobalViewer.scene;
    let data = {
      position: { x: 0, y: 0, z: 0 },
      properties: null
    };
    let feature = scene.getFeatureByMouse();
    if (feature) {
      let properties = {};
      let bbEntity = SSmap.BillboardEntity.getSelect(feature);
      if (!bbEntity) {
        return;
      }
      let nameList = feature.propertyNames();
      let size = nameList.size();

      for (let i = 0; i < size; i++) {
        let key = nameList.get(i);
        let value = feature.getProperty(key);
        properties[key] = value;
      }

      data.properties = properties;
      let point = feature.parent.position;
      // let point = pickWorldPositionByMouse(x, y);
      data.position = vec3ToJson(point);
    }
    cb && cb(data);
  },
  changeProperty(id, property) {
    let bbEntity = global.BBEntityStore.get(id);
    if (bbEntity) {
      for (const key in property) {
        if (Object.hasOwnProperty.call(property, key)) {
          const value = property[key];
          bbEntity[key] = value;
        }
      }
    }
  },
  _getBillboard(id) {
    let bbEntity = global.BBEntityStore.get(id);
    return bbEntity;
  },
  moveBillboard(id, position) {
    let bbEntity = this._getBillboard(id);
    let _position = jsonToVec3(position);
    bbEntity.position = _position;
  },
  remove(id) {
    global.BBEntityStore.has(id) && global.BBEntityStore.remove(id);
  },
  removeAll() {
    if (collection) {
      collection.removeAll();
      collection = null;
    }
  }
};
export { Billboard };
